package com.google.gwt.livinglife.client;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;

/**
 * Generic DeckPanel with a side panel containing all the links
 * When a user clicks on any of the side links, the corresponding panel in the DeckPanel
 * is made visible.
 * @author amit
 *
 */
public class DeckPanelWithSideLinks implements ValueChangeHandler {
	private HorizontalPanel main_panel = new HorizontalPanel();
	private VerticalPanel link_panel = new VerticalPanel();
	private Image vertical_bar = new Image("images/noimage.png");
  private ArrayList<Hyperlink> links = new ArrayList<Hyperlink>();
  private DeckPanel deck_panel = new DeckPanel();
  private LoginInfo login_info;
  private HashMap<String, Integer> link_map = new HashMap<String, Integer>();
  Integer widget_index = new Integer(0);
  
  public DeckPanelWithSideLinks() {
  	main_panel.add(link_panel);
  	vertical_bar.setPixelSize(3, 700);
  	main_panel.setSpacing(2);
  	main_panel.add(vertical_bar);
  	main_panel.add(deck_panel);
  	History.addValueChangeHandler(this);   
  }
  public void addWidget(Widget w ,String name, String link ) {
		deck_panel.add(w);
		link_map.put(link, widget_index);
		Hyperlink hyperlink = new Hyperlink(name, link);
		links.add(hyperlink);
		link_panel.add(hyperlink);
		if (widget_index ==0) {
			deck_panel.showWidget(0);
		}
		++widget_index;
  }
  public void set_login_info(LoginInfo info) {
  	login_info = info;
  }
	@Override
	public void onValueChange(ValueChangeEvent event) {
		String link_name = (String) event.getValue();
		showPanel(link_name);
	}
	
	public void showPanel(String link_name) {
		Integer index = link_map.get(link_name);
		login_info = LivingLife.getLogin_info();
		if (index != null && login_info != null) {
			if (!login_info.isLoggedIn()) {
				Window.alert("Please sign-in.");
			} else {
				deck_panel.showWidget(index);
			}
		}
	}
	public HorizontalPanel getMain_panel() {
		return main_panel;
	}
}
